﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using eCommerce.core;
using eCommerce.core.AdminGroup;
using eCommerce.core.Authenticated;
using eCommerce.core.Helpers;

namespace eCommerce.cms.Controls.AdminGroup
{
    public partial class AdminGroupManager : AuthenticatedControl
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            var ucId = Utils.GetIdFromRequestString(Utils.UserControlId, string.Empty);

            switch (ucId)
            {
                case "edit":
                    Control c1 = LoadControl("CreateOrEdit.ascx");
                    plcControl.Controls.Add(c1);
                    break;                
                case "agm":
                    Control c2 = LoadControl("AdminGroupMenu.ascx");
                    plcControl.Controls.Add(c2);
                    break;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindData();
        }

        private void BindData()
        {
            lvGroup.DataSource = AdminGroupController.AdminGroup_GetAll();
            lvGroup.DataBind();
        }        

        protected void lvGroup_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                var hfAdminGroupId  = (HiddenField)e.Item.FindControl("hfAdminGroupId");
                var hfStatus        = (HiddenField)e.Item.FindControl("hfStatus");
                var hlEdit          = (HyperLink)e.Item.FindControl("hlEdit");
                var lnkDelete       = (LinkButton)e.Item.FindControl("lnkDelete");

                // Nếu là quyền Administrator thì không được sửa hoặc xóa
                if (StatusHelper.IsChecked(Utils.ToInt32(hfStatus.Value), AdminGroupObject.IsAdministrator) && hlEdit != null)
                    hlEdit.Visible = false;
                else
                    hlEdit.NavigateUrl = UrlHelper.EditGroup(Utils.ToInt32(hfAdminGroupId.Value));

                if (StatusHelper.IsChecked(Utils.ToInt32(hfStatus.Value), AdminGroupObject.IsAdministrator) && lnkDelete != null)
                    lnkDelete.Visible = false;
            }
        }

        protected void lvGroup_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            try
            {
                if (string.Equals(e.CommandName, "DeleteGroup"))
                {
                    var dataItem = (ListViewDataItem)e.Item;
                    var id = Utils.ToInt32(lvGroup.DataKeys[dataItem.DisplayIndex].Value);

                    if (id > 0)
                    {
                        var group = AdminGroupController.AdminGroup_GetById(id);

                        if (group != null)
                        {
                            AdminGroupController.AdminGroup_Delete(id);

                            ltMessage.Text = Utils.ShowMessage("Xóa bản ghi thành công.", MessageType.Information);

                            BindData();
                        }
                    }
                }
            }
            catch
            {
                ltMessage.Text = Utils.ShowMessage("Bạn cần xóa các thành viên trước khi xóa nhóm", MessageType.Information);
            }
        }              
    }
}